<?php

namespace app\api\controller\wanlshop;


use app\api\model\SeparateSet;
use app\api\model\wanlshop\ShopConfig;
use app\common\controller\Api;
use fast\Random;
use wechat\MiniProgramService;

class GiftCoupon extends Api
{
    protected $noNeedLogin = '';
    protected $noNeedRight = '*';
    /**
     * 赠送
     */
    public function give()
    {
        // 根据赠送金额
        $params = $this->request->param();
        if (empty($params['to_id']) || empty($params['id']))
            $this->error("参数为空【to_id/coupon_id】");
        $this->model = model('app\api\model\wanlshop\CouponReceive');
        $count = $this->model
            ->where([
                'id' => $params['id'],
                'state' => 1,
                'user_id' => $this->auth->id
            ])->count();
        if ($count == 0)
            $this->error("该优惠券已过期");
        $this->model->where(['id'=>$params['id']])->update(['user_id'=>$params['to_id']]);
        $this->success("赠送成功");
    }

    /**
     * 赠送体验券
     */
    public function give_experience(){
        $params = $this->request->param();
        if (empty($params['to_id']) || empty($params['id']) || empty($params['price']))
            $this->error("参数为空【to_id/coupon_id】");
        $this->model = model('app\api\model\wanlshop\CouponReceive');
        $coupon = $this->model
            ->where([
                'id' => $params['id'],
                'state' => 1,
                'user_id' => $this->auth->id
            ])->find();
        if (empty($coupon))
            $this->error("该优惠券不存在");
        if ($coupon->type != 'reduction')
            $this->error("满减券才能赠送体验资格");
        if ($coupon->price < $params['price'])
            $this->error("赠送金额不能大于优惠券原本金额");
        $coupon->price -= $params['price'];
        $coupon->save();
        unset($coupon->id);
        $copy_obj_new = clone $coupon;
        $copy_obj_new->user_id = $params['to_id'];
        $copy_obj_new->price = $params['price'];
        $copy_obj_new->coupon_no = Random::alnum(16);
        $this->model->allowField(true)->save($copy_obj_new->toArray());
        $this->success("转增体验券成功");
    }

    /**
     * 获取优惠券详情
     */
    public function coupon_info(){
        $id = $this->request->get('coupon_id');
        if (empty($id))
            $this->error("参数缺失【coupon_id】");
        $this->model = model('app\api\model\wanlshop\Coupon');
        $coupon = $this->model
            ->where([
                'id' => $id,
            ])->find();
        $this->success("获取成功",$coupon);
    }

    /**
     * 生成小程序二维码
     */
    public function gencode(){
        $id = $this->request->get('id');
        if (empty($id))
            $this->error('参数缺失【id】');
        $this->model = model('app\api\model\wanlshop\CouponReceive');
        $coupon = $this->model->where(['id'=>$id,'user_id'=>$this->auth->id])->find();
        if (empty($coupon))
            $this->error("优惠券不存在");
        $shop_id = $coupon->shop_id;
        $shop_config =  ShopConfig::get(['shop_id'=>$shop_id]);
        if (empty($shop_config['service_secret']))
            $this->error("无服务商");
        $secret = SeparateSet::get(['secret'=>$shop_config->service_secret]);
        $mini = new MiniProgramService($secret);
        $res = $mini->generate_code(['path'=>'/page/index/index','param'=>$id,
            'directory'=>"/path/to/coupon/"],
            "id");
        if ($res['result_code'] == 'SUCCESS')
            $this->success("成功生成二维码","https://tp.haokeliebian.com{$res['data']}");
        else
            $this->error("生成二维码失败",$res['result_message']);
    }

}